package com.wei.news.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wei.base.base.BaseAdapter;
import com.wei.base.base.BaseFragment;
import com.wei.base.base.BaseView;
import com.wei.base.base.BaseViewHolder;
import com.wei.news.R;
import com.wei.news.config.Config;
import com.wei.news.model.NewBean;
import com.wei.news.model.NewTypeBean;
import com.wei.news.presenter.NewPresenter;
import com.wei.news.ui.activity.WebViewActivity;

import java.util.List;

import butterknife.Bind;

/**
 * 作者：赵若位
 * 时间：2018/8/1 12:57
 * 邮箱：1070138445@qq.com
 * 功能：
 */
public class NewFragment extends BaseFragment<NewPresenter> implements BaseView
{
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.refresh)
    SmartRefreshLayout mRefresh;

    private BaseAdapter mAdapter;

    private NewTypeBean mData = null;


    public static NewFragment getFragment(NewTypeBean data)
    {
        NewFragment fragment = new NewFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(NewFragment.class.getSimpleName(), data);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected NewPresenter createPresenter()
    {
        return new NewPresenter(this);
    }

    @Override
    protected int getResourceId()
    {
        return R.layout.fragment_new;
    }


    @Override
    public void initView(View rootView)
    {
        super.initView(rootView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new BaseAdapter<NewBean>(mActivity, R.layout.item_new)
        {
            @Override
            public void convert(BaseViewHolder holder, int position, final NewBean data)
            {
                holder.setText(data.getTitle(), R.id.item_title)
                        .setText(data.getAuthorName(), R.id.item_author)
                        .setText(data.getDate(), R.id.item_time)
                        .setImageResource(data.getThumbnailPicS(), R.id.item_img)
                        .itemView.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        Intent it = new Intent(mActivity, WebViewActivity.class);
                        it.putExtra(WebViewActivity.class.getSimpleName(), data);
                        startActivity(it);
                    }
                });
            }
        };
        mRecyclerView.setAdapter(mAdapter);
        setRefreshListener();
        mData = getArguments().getParcelable(NewFragment.class.getSimpleName());
        isUIVisible = true;
    }


    @Override
    protected void netWork()
    {
        super.netWork();
        if (mData == null)
        {
            return;
        }
        mPresenter.getNewsList(mData.getDescript());
    }

    /**
     * 刷新监听
     */
    private void setRefreshListener()
    {
        mRefresh.setOnRefreshListener(new OnRefreshListener()
        {
            @Override
            public void onRefresh(RefreshLayout refresh)
            {
                initData();
                mRefresh.finishRefresh(Config.SMARTREFRESH_MAX_REFRESH_TIME);
            }
        });
        mRefresh.setEnableLoadmore(false);
    }


    @Override
    public void showData(Object obj)
    {
        if (mRefresh.isRefreshing())
        {
            alert(getString(R.string.alert_refresh_success));
            mRefresh.finishRefresh();
        }
        if (obj instanceof List)
        {
            List<NewBean> list = (List<NewBean>) obj;
            mAdapter.setData(list);
        }
    }

    @Override
    public void showError(String msg)
    {
        if (mRefresh.isRefreshing())
        {
            mRefresh.finishRefresh();
        }
        alert(msg);
    }
}
